/*
 * SmartFolderPanel.java
 *
 * Created on May 23, 2009, 2:19 PM
 */

package dynamicFolders;

import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.ListModel;

/**
 * 
 * @author jeroen
 */
public class SmartFolderPanel extends javax.swing.JPanel {

	/** Creates new form SmartFolderPanel */
	public SmartFolderPanel() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jScrollPane3 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jScrollPane1 = new javax.swing.JScrollPane();
		jListTable = new javax.swing.JList();
		jScrollPane2 = new javax.swing.JScrollPane();
		jListColumn = new javax.swing.JList();
		jTextFieldTable = new javax.swing.JTextField();
		jTextFieldColumn = new javax.swing.JTextField();
		jTextFieldValue = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jButtonView = new javax.swing.JButton();
		jButtonAdd = new javax.swing.JButton();
		jButtonModify = new javax.swing.JButton();
		jButtonRemove = new javax.swing.JButton();
		jButtonClear = new javax.swing.JButton();
		jScrollPane4 = new javax.swing.JScrollPane();
		jTableData = new javax.swing.JTable();
		jComboBoxConstraint = new javax.swing.JComboBox();
		jLabel6 = new javax.swing.JLabel();
		
		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] { { null, null, null, null },
						{ null, null, null, null }, { null, null, null, null },
						{ null, null, null, null } }, new String[] { "Title 1",
						"Title 2", "Title 3", "Title 4" }));
		jScrollPane3.setViewportView(jTable1);

		setPreferredSize(new java.awt.Dimension(810, 948));

		jListTable.setModel(new javax.swing.AbstractListModel() {
			String[] strings = { "Course", "File", "Task", "Lesson", "Event",
					"Exam", "Project", "Student", "Teacher", "Contact" };

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		jListTable
				.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
					public void valueChanged(
							javax.swing.event.ListSelectionEvent evt) {
						jListTableValueChanged(evt);
					}
				});
		jScrollPane1.setViewportView(jListTable);

		jListColumn.setModel(initList);
		jListColumn
				.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
					public void valueChanged(
							javax.swing.event.ListSelectionEvent evt) {
						jListColumnValueChanged(evt);
					}
				});
		jScrollPane2.setViewportView(jListColumn);

		jLabel1.setText("Tabellen");

		jLabel2.setText("Kolommen");

		jLabel3.setText("Tabel:");

		jLabel4.setText("Kolom:");

		jLabel5.setText("Waarde:");

		jButtonView.setText("Data");
		jButtonView.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonViewActionPerformed(evt);
			}
		});
		
		jButtonClear.setText("Clear");
		jButtonClear.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonClearActionPerformed(evt);
			}
		});

		jButtonAdd.setText("Toevoegen");
		jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonAddActionPerformed(evt);
			}
		});

		jButtonModify.setText("Wijzigen");
		jButtonModify.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonModifyActionPerformed(evt);
			}
		});

		jButtonRemove.setText("Verwijderen");
		jButtonRemove.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonRemoveActionPerformed(evt);
			}
		});

		jScrollPane4.setViewportView(jTableData);

		jComboBoxConstraint.setModel(new javax.swing.DefaultComboBoxModel(
				new String[] { "geen", "<", "<=", ">", ">=", "=" }));

		jLabel6.setText("Voorwaarde:");
		
		jTableData.setRowHeight(20);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jScrollPane1,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jLabel1))
																		.addGap(
																				22,
																				22,
																				22)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addComponent(
																												jScrollPane2,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												80,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																jLabel4)
																														.addComponent(
																																jLabel3)
																														.addComponent(
																																jLabel6)
																														.addComponent(
																																jLabel5))
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.LEADING,
																																false)
																														.addComponent(
																																jTextFieldValue,
																																javax.swing.GroupLayout.Alignment.TRAILING)
																														.addComponent(
																																jTextFieldColumn,
																																javax.swing.GroupLayout.Alignment.TRAILING)
																														.addComponent(
																																jComboBoxConstraint,
																																0,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																Short.MAX_VALUE)
																														.addComponent(
																																jTextFieldTable,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																103,
																																javax.swing.GroupLayout.PREFERRED_SIZE)))
																						.addComponent(
																								jLabel2)))
														.addComponent(
																jScrollPane4,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																786,
																Short.MAX_VALUE)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jButtonView)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButtonAdd)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButtonModify)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButtonRemove)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																						jButtonClear)))
										.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(4, 4, 4)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(jLabel2))
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createSequentialGroup()
																		.addGap(
																				15,
																				15,
																				15)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel3)
																						.addComponent(
																								jTextFieldTable,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jLabel4))
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								jScrollPane2)
																						.addComponent(
																								jScrollPane1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								261,
																								Short.MAX_VALUE)
																						.addGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								layout
																										.createSequentialGroup()
																										.addComponent(
																												jTextFieldColumn,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.BASELINE)
																														.addComponent(
																																jLabel6)
																														.addComponent(
																																jComboBoxConstraint,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.PREFERRED_SIZE))
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING)
																														.addComponent(
																																jLabel5)
																														.addComponent(
																																jTextFieldValue,
																																javax.swing.GroupLayout.PREFERRED_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.PREFERRED_SIZE))
																										.addGap(
																												137,
																												137,
																												137)))))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jButtonView)
														.addComponent(
																jButtonAdd)
														.addComponent(
																jButtonModify)
														.addComponent(
																jButtonRemove)
														.addComponent(
																jButtonClear))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jScrollPane4,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												380,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(465, Short.MAX_VALUE)));
	}// </editor-fold>//GEN-END:initComponents

	private void jListTableValueChanged(javax.swing.event.ListSelectionEvent evt) {// GEN-FIRST:event_jListTableValueChanged
		int index = jListTable.getSelectedIndex();
		switch (index) {
		case 0:
			jListColumn.setModel(makeCourseListModel());
			break;
		case 1:
			jListColumn.setModel(makeFileListModel());
			break;
		case 2:
			jListColumn.setModel(makeTaskListModel());
			break;
		case 3:
			jListColumn.setModel(makeLessonListModel());
			break;
		case 4:
			jListColumn.setModel(makeEventListModel());
			break;
		case 5:
			jListColumn.setModel(makeExamListModel());
			break;
		case 6:
			jListColumn.setModel(makeProjectListModel());
			break;
		case 7:
			jListColumn.setModel(makeStudentListModel());
			break;
		case 8:
			jListColumn.setModel(makeTeacherListModel());
			break;
		case 9:
			jListColumn.setModel(makeContactSFListModel());
			break;
		default:
			System.out.println("invalid selectionsomehow");
			break;
		}
	}// GEN-LAST:event_jListTableValueChanged

	public ListModel makeContactSFListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "Email");
		lst.add(1, "Name");
		lst.add(2, "Forename");
		lst.add(3, "PhoneNumber");
		lst.add(4, "MoreInformation");
		return lst;
	}

	public ListModel makeStudentListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "Email");
		lst.add(1, "Name");
		lst.add(2, "Forename");
		lst.add(3, "PhoneNumber");
		lst.add(4, "MoreInformation");
		lst.add(5, "AlternativeEmail");
		return lst;
	}

	public ListModel makeTeacherListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "Email");
		lst.add(1, "Name");
		lst.add(2, "Forename");
		lst.add(3, "PhoneNumber");
		lst.add(4, "MoreInformation");
		lst.add(5, "Title");
		lst.add(6, "Office");
		return lst;
	}

	public ListModel makeProjectListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "Name");
		lst.add(1, "Description");
		lst.add(2, "Grades");
		lst.add(3, "MoreInformation");
		lst.add(4, "Planning");
		return lst;
	}

	public ListModel makeExamListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "Name");
		lst.add(1, "Description");
		lst.add(2, "Grades");
		lst.add(3, "MoreInformation");
		lst.add(4, "Room");
		lst.add(5, "Hours");
		lst.add(6, "Type");
		lst.add(7, "Oral");
		return lst;
	}

	public ListModel makeEventListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "Summary");
		lst.add(1, "Start");
		lst.add(2, "End");
		lst.add(3, "IsDeadline");
		lst.add(4, "WarningDate");
		lst.add(5, "MoreInformation");
		return lst;
	}

	public ListModel makeLessonListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "Room");
		lst.add(1, "DayOfWeek");
		lst.add(2, "NumberOfWeeks");
		lst.add(3, "Start");
		lst.add(4, "End");
		lst.add(5, "Summary");
		lst.add(6, "MoreInformation");
		return lst;
	}

	public ListModel makeCourseListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "Name");
		lst.add(1, "Description");
		lst.add(2, "BeginYear");
		lst.add(3, "Semester");
		lst.add(4, "MoreInformation");
		return lst;
	}

	public ListModel makeFileListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "PathName");
		lst.add(1, "MoreInformation");
		return lst;
	}

	public ListModel makeTaskListModel() {
		DefaultListModel lst = new DefaultListModel();
		lst.add(0, "Name");
		lst.add(1, "Description");
		lst.add(2, "Done");
		lst.add(3, "Grades");
		lst.add(4, "MoreInformation");
		return lst;
	}

	private void jButtonViewActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonViewActionPerformed
		if (jButtonView.getText().toString() == "Data"){
			if (queryView.getNrColumns() != 0) {
				jButtonAdd.setEnabled(false);
				jButtonModify.setEnabled(false);
				jButtonRemove.setEnabled(false);
				jButtonClear.setEnabled(false);

				generateDataView();
				jButtonView.setText("Kolommen");
			}
		} else {
			jButtonAdd.setEnabled(true);
			jButtonModify.setEnabled(true);
			jButtonRemove.setEnabled(true);
			jButtonClear.setEnabled(true);
			
			generateQueryView();
			jButtonView.setText("Data");
		}

	}// GEN-LAST:event_jButtonViewActionPerformed

	private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonAddActionPerformed
		
		String column = jTextFieldColumn.getText();
		String table = jTextFieldTable.getText();
		if (column.length() != 0) {
			Column c = new Column(column, table);

			if (jComboBoxConstraint.getSelectedIndex() != 0) {
				int indexConstraint = jComboBoxConstraint.getSelectedIndex();
				switch (indexConstraint) {
				case 1:
					c.addSmallerConstraint(Integer.valueOf(
							jTextFieldValue.getText()).intValue());
					break;
				case 2:
					c.addSmallerEqualConstraint(Integer.valueOf(
							jTextFieldValue.getText()).intValue());
					break;
				case 3:
					c.addBiggerConstraint(Integer.valueOf(
							jTextFieldValue.getText()).intValue());
					break;
				case 4:
					c.addBiggerEqualConstraint(Integer.valueOf(
							jTextFieldValue.getText()).intValue());
					break;
				case 5:if(isInt(jTextFieldValue.getText())){
					c.addEqualConstraint(Integer.valueOf(
							jTextFieldValue.getText()).intValue());
				}else{
					c.addEqualConstraint(
							jTextFieldValue.getText().toString());
				}
				break;
				}

			}
			try {
				queryView.addColumn(c);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}

			
			generateQueryView();
			System.out.println(queryView.createQuerry());
		}
		// jTableData.setModel(new
		// javax.swing.table.DefaultTableModel(rowData,header));
	}// GEN-LAST:event_jButtonAddActionPerformed
	
	public boolean isInt(String s){
		boolean test = true;
		try{
			int value = Integer.parseInt(s);
			
		}catch(Exception e){
			test = false;
		}
		return test;
	}
	
	public void generateDataView() {
		Vector<String> header = new Vector();
		header = queryView.generateHeader();
		Vector<Vector<String>> rowData = new Vector();
		try {
			rowData = queryView.querryDb();
		} catch (Exception e) {
			
			e.printStackTrace();
		}

		jTableData.setModel(new javax.swing.table.DefaultTableModel(rowData,
				header));
	}

	public void generateQueryView() {
		Vector<String> header = new Vector();
		header = queryView.generateHeader();
		Vector<Vector<String>> rowData = new Vector();
		rowData = queryView.generateViewBody();

		jTableData.setModel(new javax.swing.table.DefaultTableModel(rowData,
				header));
	}

	private void jButtonModifyActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonModifyActionPerformed
		int col;		
		if((col = jTableData.getSelectedColumn())  != -1){
			String column = jTextFieldColumn.getText();
			String table = jTextFieldTable.getText();
			if (column.length() != 0) {
				Column c = new Column(column, table);
				if (jComboBoxConstraint.getSelectedIndex() != 0) {
					int indexConstraint = jComboBoxConstraint.getSelectedIndex();
					switch (indexConstraint) {
					case 1:
						c.addSmallerConstraint(Integer.valueOf(
								jTextFieldValue.getText()).intValue());
						break;
					case 2:
						c.addSmallerEqualConstraint(Integer.valueOf(
								jTextFieldValue.getText()).intValue());
						break;
					case 3:
						c.addBiggerConstraint(Integer.valueOf(
								jTextFieldValue.getText()).intValue());
						break;
					case 4:
						c.addBiggerEqualConstraint(Integer.valueOf(
								jTextFieldValue.getText()).intValue());
						break;
					case 5:if(isInt(jTextFieldValue.getText())){
						c.addEqualConstraint(Integer.valueOf(
								jTextFieldValue.getText()).intValue());
					}else{
						c.addEqualConstraint(
								jTextFieldValue.getText().toString());
					}
					break;
					}

				}
				queryView.insertColumn(c, col);
			}
			generateQueryView();
		}		
	}// GEN-LAST:event_jButtonModifyActionPerformed
	
	private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonModifyActionPerformed
		
		
		queryView = new View();
		generateQueryView();
	}// GEN-LAST:event_jButtonModifyActionPerformed
	
	private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonRemoveActionPerformed
		
		int col;
		
		if((col = jTableData.getSelectedColumn())  != -1){
			queryView.deleteColumn(col);
			generateQueryView();
		}
	}// GEN-LAST:event_jButtonRemoveActionPerformed

	private void jListColumnValueChanged(
			javax.swing.event.ListSelectionEvent evt) {// GEN-FIRST:event_jListColumnValueChanged
		
		jTextFieldColumn.setText(jListColumn.getSelectedValue().toString());
		jTextFieldTable.setText(jListTable.getSelectedValue().toString());
		jComboBoxConstraint.setSelectedIndex(0);
		jTextFieldValue.setText(null);
	}// GEN-LAST:event_jListColumnValueChanged

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButtonAdd;
	private javax.swing.JButton jButtonModify;
	private javax.swing.JButton jButtonRemove;
	private javax.swing.JButton jButtonView;
	private javax.swing.JButton jButtonClear;
	private javax.swing.JComboBox jComboBoxConstraint;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JList jListColumn;
	private javax.swing.JList jListTable;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JScrollPane jScrollPane4;
	private javax.swing.JTable jTable1;
	private javax.swing.JTable jTableData;
	private javax.swing.JTextField jTextFieldColumn;
	private javax.swing.JTextField jTextFieldTable;
	private javax.swing.JTextField jTextFieldValue;
	// End of variables declaration//GEN-END:variables
	// column class
	View queryView = new View();
	DefaultListModel initList = new DefaultListModel();
	

}
